package com.godpaper.the5elements.busniess.managers
{
	import com.godpaper.the5elements.model.BoardModel;
	import com.godpaper.the5elements.model.PiecesModel;
	import com.godpaper.the5elements.model.PlayerModel;
	import com.godpaper.the5elements.views.components.GasketButton;
	import com.godpaper.the5elements.views.components.PieceButton;
	
	import mx.collections.ArrayCollection;

	public class BoardManager
	{
		public function BoardManager()
		{
		}
		/**
		 *repopulate board selected piece's status. 
		 */		
		public static function repopulation():void
		{
			//reset all pieces and gaskets
			PiecesModel.getInstance().selectedPiece.selected = false;
			PiecesModel.getInstance().selectedPiece = null;
		}
		/**
		 * 
		 * @param fireGBs
		 * @return the postive of addressable pieceButtons' position; 
		 * 
		 */		
		public static function triggleAddressableEarthPosition(fireGBs:ArrayCollection):void
		{
			for(var i:int=0;i<fireGBs.length;i++)
			{
				var fireGB:GasketButton = fireGBs.getItemAt( i ) as GasketButton;
				//left
				if(fireGB.left)
				{
					var earthPBleft:PieceButton = BoardModel.getInstance().gamePosition.gett( fireGB.left.position[0],
																							  fireGB.left.position[1]) as PieceButton;	
					if(earthPBleft)
					{
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
						{
							if(earthPBleft.styleName==PieceButton.BUTTON_STYLE_NAME_RED)
							{
								fireGB.left.enabled = true;
							}
						}else
						{
							if(earthPBleft.styleName==PieceButton.BUTTON_STYLE_NAME_BLUE)
							{
								fireGB.left.enabled = true;
							}
						}
					}
				}		
				//top
				if(fireGB.top)
				{
					var earthPBtop:PieceButton = BoardModel.getInstance().gamePosition.gett( fireGB.top.position[0],
																							 fireGB.top.position[1]) as PieceButton;	
					if(earthPBtop)
					{
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
						{
							if(earthPBtop.styleName==PieceButton.BUTTON_STYLE_NAME_RED)
							{
								fireGB.top.enabled = true;
							}
						}else
						{
							if(earthPBtop.styleName==PieceButton.BUTTON_STYLE_NAME_BLUE)
							{
								fireGB.top.enabled = true;
							}
						}
					}
				}		
				//right
				if(fireGB.right)
				{
					var earthPBright:PieceButton = BoardModel.getInstance().gamePosition.gett( fireGB.right.position[0],
																							   fireGB.right.position[1]) as PieceButton;	
					if(earthPBright)
					{
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
						{
							if(earthPBright.styleName==PieceButton.BUTTON_STYLE_NAME_RED)
							{
								fireGB.right.enabled = true;
							}
						}else
						{
							if(earthPBright.styleName==PieceButton.BUTTON_STYLE_NAME_BLUE)
							{
								fireGB.right.enabled = true;
							}
						}
					}
				}		
				//bottom
				if(fireGB.bottom)
				{
					var earthPBbottom:PieceButton = BoardModel.getInstance().gamePosition.gett( fireGB.bottom.position[0],
																							    fireGB.bottom.position[1]) as PieceButton;	
					if(earthPBbottom)
					{
						if(PlayerModel.getInstance().gameTurnStatus==PlayerModel.getInstance().IS_BLUE_TURN_NOW)
						{
							if(earthPBbottom.styleName==PieceButton.BUTTON_STYLE_NAME_RED)
							{
								fireGB.bottom.enabled = true;
							}
						}else
						{
							if(earthPBbottom.styleName==PieceButton.BUTTON_STYLE_NAME_BLUE)
							{
								fireGB.bottom.enabled = true;
							}
						}
					}
				}																	      
			}
		}
		
	}
}